from flask import Flask, request

import et
import wps
import wpp

app = Flask(__name__)


class DataService:
    def __init__(self):
        # 在这里初始化你的数据服务对象
        self.data = {'example': 'Hello, World!'}

    def get_data(self):
        return self.data

    def set_data(self, key, value):
        self.data[key] = value


# 在应用启动时创建数据服务对象
data_service = DataService()


@app.before_first_request
def initialize_data():
    # 在第一次请求前执行初始化操作
    global data_service
    data_service = DataService()
    data_service.set_data("wps", wps.init_wps())
    data_service.set_data("wpp", wpp.init_wpp())
    data_service.set_data("et", et.init_et())


@app.route("/test", methods=['get'])
def test():
    global data_service
    data = data_service.get_data()
    docs = data['wps']


@app.route("/convert", methods=['post'])
def convert():
    if not request.get_json():
        return '{"status":"500"}'
    data = request.get_json()
    print(data)
    type = data['type']
    global data_service
    app = data_service.get_data()[type]

    if type == 'wps':
        print(data['path'])
        wps.convert_file(data['path'], app.Documents, data['format'])
    elif type == 'et':
        print(data)
        et.convert_file(data['path'], app.Workbooks, data['format'])
    elif type == 'wpp':
        print(data)
        wpp.convert_file(data['path'], app.Presentations, data['format'])
    else:
        return '{"status":"500"}'
    return '{"status":"200"}'


if __name__ == '__main__':
    app.run()
